home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxhint1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.1 KB  |  270 lines

  1. /* Copyright (C) 1990, 1992, 1993, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxhint1.c,v 1.2 2000/09/19 19:00:37 lpd Exp $ */
  20. /* Font level hints for Type 1 fonts */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxarith.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gxfont.h"
  27. #include "gxfont1.h"
  28. #include "gxtype1.h"
  29.  
  30. /* Define whether to use font hints. */
  31. /* Only set this to false for debugging the hint machinery. */
  32. static bool USE_HINTS = true;
  33.  
  34. /* ------ Initialization ------ */
  35.  
  36. typedef zone_table(1) a_zone_table;
  37. typedef stem_table(1) a_stem_table;
  38. private void
  39.     compute_snaps(P6(const gs_matrix_fixed *, const a_stem_table *,
  40.              stem_snap_table *, int, int, const char *));
  41. private alignment_zone *
  42.     compute_zones(P6(const gs_matrix_fixed *, const font_hints *,
  43.     const a_zone_table *, const a_zone_table *, alignment_zone *, int));
  44. private int
  45.     transform_zone(P4(const gs_matrix_fixed *, const font_hints *,
  46.               const float *, alignment_zone *));
  47.  
  48. /* Reset the font-level hints. */
  49. void
  50. reset_font_hints(font_hints * pfh, const gs_log2_scale_point * plog2_scale)
  51. {
  52.     set_pixel_scale(&pfh->scale.x, plog2_scale->x);
  53.     set_pixel_scale(&pfh->scale.y, plog2_scale->y);
  54.     pfh->axes_swapped = pfh->x_inverted = pfh->y_inverted = false;
  55.     pfh->use_x_hints = pfh->use_y_hints = false;
  56.     pfh->snap_h.count = pfh->snap_v.count = 0;
  57.     pfh->a_zone_count = 0;
  58. }
  59.  
  60. /* Compute the font-level hints from the font and the matrix. */
  61. /* We should cache this with the font/matrix pair.... */
  62. void
  63. compute_font_hints(font_hints * pfh, const gs_matrix_fixed * pmat,
  64.        const gs_log2_scale_point * plog2_scale, const gs_type1_data * pdata)
  65. {
  66.     alignment_zone *zp = &pfh->a_zones[0];
  67.  
  68.     reset_font_hints(pfh, plog2_scale);
  69.     /* Figure out which hints, if any, to use, */
  70.     /* and the orientation of the axes. */
  71.     if (is_fzero(pmat->xy))
  72.     pfh->y_inverted = is_fneg(pmat->yy),
  73.         pfh->use_y_hints = USE_HINTS;
  74.     else if (is_fzero(pmat->xx))
  75.     pfh->y_inverted = is_fneg(pmat->xy),
  76.         pfh->axes_swapped = true,
  77.         pfh->use_y_hints = USE_HINTS;
  78.     if (is_fzero(pmat->yx))
  79.     pfh->x_inverted = is_fneg(pmat->xx),
  80.         pfh->use_x_hints = USE_HINTS;
  81.     else if (is_fzero(pmat->yy))
  82.     pfh->x_inverted = is_fneg(pmat->yx),
  83.         pfh->axes_swapped = true,
  84.         pfh->use_x_hints = USE_HINTS;
  85.     if_debug6('y', "[y]ctm=[%g %g %g %g %g %g]\n",
  86.           pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  87.           pmat->tx, pmat->ty);
  88.     if_debug7('y', "[y]scale=%d/%d, swapped=%d, x/y_hints=%d,%d, x/y_inverted=%d,%d\n",
  89.           1 << plog2_scale->x, 1 << plog2_scale->y,
  90.           pfh->axes_swapped, pfh->use_x_hints, pfh->use_y_hints,
  91.           pfh->x_inverted, pfh->y_inverted);
  92.     /* Transform the actual hints. */
  93.     if (pfh->use_x_hints) {
  94.     compute_snaps(pmat, (const a_stem_table *)&pdata->StdHW,
  95.               &pfh->snap_h, 0, pfh->axes_swapped, "h");
  96.     compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapH,
  97.               &pfh->snap_h, 0, pfh->axes_swapped, "h");
  98.     }
  99.     if (pfh->use_y_hints) {
  100.     gs_fixed_point vw;
  101.     fixed *vp = (pfh->axes_swapped ? &vw.x : &vw.y);
  102.     pixel_scale *psp =
  103.     (pfh->axes_swapped ? &pfh->scale.x : &pfh->scale.y);
  104.  
  105.     /* Convert BlueFuzz to device pixels. */
  106.     if (gs_distance_transform2fixed(pmat, 0.0,
  107.                     (float)pdata->BlueFuzz,
  108.                     &vw) < 0
  109.         )
  110.         vw.x = vw.y = fixed_0;
  111.     pfh->blue_fuzz = any_abs(*vp);
  112.     /*
  113.      * Decide whether to suppress overshoots.  The formula in
  114.      * section 5.6 of the "Adobe Type 1 Font Format" says that
  115.      * at 300 dpi, if BlueScale = (P - 0.49) / 240, overshoot
  116.      * suppression turns off at point sizes at least P, i.e.:
  117.      *      P >= BlueScale * 240 + 0.49.
  118.      * At 300 dpi, P = |CTM.yy| / (300/72), so the condition is
  119.      * equivalent to
  120.      *      |CTM.yy| >= BlueScale * 1000 + 2.0417,
  121.      * or
  122.      *      BlueScale >= (|CTM.yy| - 2.0417) / 1000.
  123.      * Since *pmat is the concatenation of the FontMatrix and
  124.      * CTM, if we assume a 1000-unit scale, this is equivalent to
  125.      *      BlueScale >= |pmat->yy| - 0.00020417.
  126.      * Since the constant term is slightly smaller than
  127.      * fixed_epsilon, we just disregard it.
  128.      *
  129.      * According to the same section of the Adobe documentation,
  130.      * there is a requirement that BlueScale times the maximum
  131.      * alignment zone height must be less than 1.  We enforced
  132.      * this when the font was constructed (in zfont1.c).
  133.      */
  134.     if (gs_distance_transform2fixed(pmat, 0.0, 1.0, &vw) < 0)
  135.         vw.x = vw.y = fixed_0;
  136.     pfh->suppress_overshoot =
  137.         fixed2float(any_abs(*vp) >> psp->log2_unit) <
  138.         pdata->BlueScale;
  139.     if (gs_distance_transform2fixed(pmat, 0.0, pdata->BlueShift,
  140.                     &vw) < 0
  141.         )
  142.         vw.x = vw.y = fixed_0;
  143.     pfh->blue_shift = any_abs(*vp);
  144.     /*
  145.      * Don't let the blue shift exceed half a pixel.
  146.      * See the discussion of BlueShift in section 5.7 of the
  147.      * "Adobe Type 1 Font Format" book.
  148.      */
  149.     if (pfh->blue_shift > psp->half)
  150.         pfh->blue_shift = psp->half;
  151.     if_debug6('y', "[y]blue_fuzz=%d->%g, blue_scale=%g, blue_shift=%g->%g, sup_ov=%d\n",
  152.           pdata->BlueFuzz, fixed2float(pfh->blue_fuzz),
  153.           pdata->BlueScale,
  154.           pdata->BlueShift, fixed2float(pfh->blue_shift),
  155.           pfh->suppress_overshoot);
  156.     zp = compute_zones(pmat, pfh,
  157.                (const a_zone_table *)&pdata->BlueValues,
  158.                (const a_zone_table *)&pdata->FamilyBlues,
  159.                zp, 1);
  160.     zp = compute_zones(pmat, pfh,
  161.                (const a_zone_table *)&pdata->OtherBlues,
  162.                (const a_zone_table *)&pdata->FamilyOtherBlues,
  163.                zp, max_OtherBlues);
  164.     compute_snaps(pmat, (const a_stem_table *)&pdata->StdVW,
  165.               &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  166.     compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapV,
  167.               &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  168.     }
  169.     pfh->a_zone_count = zp - &pfh->a_zones[0];
  170. }
  171.  
  172. /* Transform one set of stem snap widths. */
  173. private void
  174. compute_snaps(const gs_matrix_fixed * pmat, const a_stem_table * pst,
  175.         stem_snap_table * psst, int from_y, int to_y, const char *tname)
  176. {
  177.     gs_fixed_point wxy;
  178.     fixed *wp = (to_y ? &wxy.y : &wxy.x);
  179.     int i;
  180.     int j = psst->count;
  181.  
  182.     for (i = 0; i < pst->count; i++) {
  183.     float w = pst->values[i];
  184.     int code =
  185.     (from_y ?
  186.      gs_distance_transform2fixed(pmat, 0.0, w, &wxy) :
  187.      gs_distance_transform2fixed(pmat, w, 0.0, &wxy)
  188.     );
  189.  
  190.     if (code < 0)
  191.         continue;
  192.     psst->data[j] = any_abs(*wp);
  193.     if_debug3('y', "[y]snap_%s[%d]=%g\n", tname, j,
  194.           fixed2float(psst->data[j]));
  195.     j++;
  196.     }
  197.     psst->count = j;
  198. }
  199.  
  200. /* Compute the alignment zones for one set of 'blue' values. */
  201. private alignment_zone *
  202. compute_zones(const gs_matrix_fixed * pmat, const font_hints * pfh,
  203.           const a_zone_table * blues, const a_zone_table * family_blues,
  204.           alignment_zone * zp, int bottom_count)
  205. {
  206.     int i;
  207.     fixed fuzz = pfh->blue_fuzz;
  208.     int inverted =
  209.     (pfh->axes_swapped ? pfh->x_inverted : pfh->y_inverted);
  210.  
  211.     for (i = 0; i < blues->count; i += 2) {
  212.     const float *vp = &blues->values[i];
  213.  
  214.     zp->is_top_zone = i >> 1 >= bottom_count;
  215.     if (transform_zone(pmat, pfh, vp, zp) < 0)
  216.         continue;
  217.     if_debug5('y', "[y]blues[%d]=%g,%g -> %g,%g\n",
  218.           i >> 1, vp[0], vp[1],
  219.           fixed2float(zp->v0), fixed2float(zp->v1));
  220.     if (i < family_blues->count) {    /* Check whether family blues should supersede. */
  221.         alignment_zone fz;
  222.         const float *fvp = &family_blues->values[i];
  223.         fixed unit = (pfh->axes_swapped ?
  224.               pfh->scale.x.unit : pfh->scale.y.unit);
  225.         fixed diff;
  226.  
  227.         if (transform_zone(pmat, pfh, fvp, &fz) < 0)
  228.         continue;
  229.         if_debug5('y', "[y]f_blues[%d]=%g,%g -> %g,%g\n",
  230.               i >> 1, fvp[0], fvp[1],
  231.               fixed2float(fz.v0), fixed2float(fz.v1));
  232.         diff = (zp->v1 - zp->v0) - (fz.v1 - fz.v0);
  233.         if (diff > -unit && diff < unit)
  234.         zp->v0 = fz.v0, zp->v1 = fz.v1;
  235.     }
  236.     /* Compute the flat position, and add the fuzz. */
  237.     if ((inverted ? zp->is_top_zone : !zp->is_top_zone))
  238.         zp->flat = zp->v1, zp->v0 -= fuzz;
  239.     else
  240.         zp->flat = zp->v0, zp->v1 += fuzz;
  241.     zp++;
  242.     }
  243.     return zp;
  244. }
  245.  
  246. /* Transform a single alignment zone to device coordinates, */
  247. /* taking axis swapping into account. */
  248. private int
  249. transform_zone(const gs_matrix_fixed * pmat, const font_hints * pfh,
  250.            const float *vp, alignment_zone * zp)
  251. {
  252.     gs_fixed_point p0, p1;
  253.     fixed v0, v1;
  254.     int code;
  255.  
  256.     if ((code = gs_point_transform2fixed(pmat, 0.0, vp[0], &p0)) < 0 ||
  257.     (code = gs_point_transform2fixed(pmat, 0.0, vp[1], &p1)) < 0
  258.     )
  259.     return code;
  260.     if (pfh->axes_swapped)
  261.     v0 = p0.x, v1 = p1.x;
  262.     else
  263.     v0 = p0.y, v1 = p1.y;
  264.     if (v0 <= v1)
  265.     zp->v0 = v0, zp->v1 = v1;
  266.     else
  267.     zp->v0 = v1, zp->v1 = v0;
  268.     return 0;
  269. }
  270.